package com.pmsystem.controller;



import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.User;
import com.pmsystem.service.AdministratorService;
import com.pmsystem.service.UserService;
import com.pmsystem.util.BeanUtil;


@Controller
@RequestMapping("admin")
public class AdministratorController {

	@Autowired
	private AdministratorService administratorService;
	@Autowired
	private UserService userService;

	/**
	 * 
	 * <p>Title: login</p >  
	 * <p>Description: 二级管理员登陆</p >  
	 * @author 泽强
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request,String username,String password) {
		Administrator admin = null;
		admin = administratorService.login(username,password);
		if (admin != null) {
			Byte enable=admin.getEnable();
			if(enable==1) {
				request.getSession().setAttribute("admin", admin);
				return "admin/index";
			}
			request.setAttribute("msg", "账号不可用");
		}else {
			request.setAttribute("msg", "账号或密码错误");
		}
		return "admin/login";
	}

	
	/**
	 * 
	 * <p>Title: login</p>
	 * <p>Description:  二级管理员退出</p> 
	 * @author 泽强
	 * @param session
	 * @param admin
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.invalidate();
		return "login";
	}
	
	/**
	 * 
	 * <p>Title: update</p>
	 * <p>Description:  修改二级管理员信息</p> 
	 * @author 泽强
	 * @param admin
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@RequestParam Map<String,Object> map) {
		Administrator admin =BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Administrator.class);
		boolean bool = administratorService.updateAdministrator(admin);
		if(bool) {
			return "success";
		}
		return "error";
	}
	/**
	 * <p>Title: findUserUncheck</p >  
	 * <p>Description:查询所有待审核用户信息</p >  
	 * @author 魏家洛
	 * @return
	 */
	@RequestMapping("findUserUncheck")
	@ResponseBody
	public List<User> findUserUncheck(){
		return administratorService.findUserUncheck();
	}
	/**
	 * <p>Title: passUser</p >  
	 * <p>Description:通过业主注册账号的审核</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return
	 */
	@RequestMapping("passUser")
	@ResponseBody
	public boolean passUser(String userId) {
		return administratorService.passUser(userId);
	}
	/**
	 * <p>Title: deleteUser</p >  
	 * <p>Description:删除用户或不通过业主账号注册申请</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return
	 */
	@RequestMapping("deleteUser")
	@ResponseBody
	public boolean deleteUser(String userId) {
		return administratorService.deleteUser(userId);
	}
}
